package com.sunbeam.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import com.sunbeam.common.consts.EConstants;

/**
 * 
 * @ClassName: DBUtils
 * @Description: TODO
 * @author AromaSunbeam lyzpoq_gmail_com
 * @date 2013-8-31 下午10:59:44
 * 
 */
public class DBUtils {
	private static String m_url = null;
	private static String m_username = null;
	private static String m_password = null;
	private static String driver_name = null;
	private static Connection m_conn = null;
	private final static DBUtils m_su = new DBUtils();

	/**
	 * 
	 * Title: Description:
	 */
	private DBUtils() {

	}

	/**
	 * 
	 * @Title: getConnection
	 * @Description: Get Connection
	 * @param @return
	 * @return Connection
	 * @throws
	 */
	public static Connection getConnection() {
		try {
			if (m_conn == null || m_conn.isClosed()) {

				Class.forName(driver_name);

				if (m_url == null || m_username == null || m_password == null) {
					getUrlParams();
				}
				m_conn = DriverManager.getConnection(m_url, m_username,
						m_password);// 2000

			}
		} catch (Exception ex) {
			ex.printStackTrace();

		}
		return m_conn;
	}

	/**
	 * 
	 * @Title: newConnection
	 * @Description: TODO
	 * @param @return
	 * @return Connection
	 * @throws
	 */
	public static Connection newConnection() {
		Connection newConn = null;
		try {
			Class.forName(driver_name);

			if (m_url == null || m_username == null || m_password == null) {
				getUrlParams();
			}
			newConn = DriverManager
					.getConnection(m_url, m_username, m_password);// 2000

		} catch (Exception ex) {
			ex.printStackTrace();

		}
		return newConn;
	}

	/**
	 * 
	 * @Title: closeConnection
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 */
	public static void closeConnection() {
		if (m_conn != null) {
			try {
				m_conn.close();
				m_conn = null;
			} catch (SQLException ex) {
				ex.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @Title: getUrlParams
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 */
	@SuppressWarnings("unchecked")
	public static void getUrlParams() {
		try {
			m_url = "";
			m_username = "";
			m_password = "";
			String value = "";
			HashMap<String, String> databaseMap = new HashMap<String, String>();
			FileInputStream in = null;
			Properties props = new Properties();
			try {
				String databasePropertyName = StringUtils
						.URLPathCombine(new String[] {
								DBUtils.class.getClassLoader().getResource("").getPath(),
								EConstants.DEFAULT_SETTINGS_DIRECTORY,
								EConstants.DEFAULT_DATABASE_PROPERTIES });
				in = new FileInputStream(new File(databasePropertyName));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				props.load(in);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (props.isEmpty()) {
				System.out.println("Please config Properties");
			}
			@SuppressWarnings("rawtypes")
			Set set = props.keySet();
			Iterator<String> it = set.iterator();
			while (it.hasNext()) {
				String key = it.next();
				value = props.getProperty(key).trim();
				databaseMap.put(key, value);
			}
			m_url = databaseMap.get("m_url");
			m_username = databaseMap.get("m_username");
			m_password = databaseMap.get("m_password");
			driver_name = databaseMap.get("driver_name");
			if (m_url == null || m_username == null || m_password == null
					|| driver_name == null) {
				System.out.println("Please config database.properties");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Title: getInstance
	 * @Description: TODO
	 * @param @return
	 * @return DBUtils
	 * @throws
	 */
	public static DBUtils getInstance() {
		return m_su;
	}

}